package com.andooco.dataq.controller;

import com.alibaba.fastjson.JSONObject;
import com.andooco.dataq.service.func.dto.FuncDto;
import com.andooco.dataq.service.jdbc.dto.JdbcDto;
import com.andooco.dataq.service.query.QueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 数据查询管理
 *
 * @author andooco
 * 20201127
 */
@Slf4j
@Controller
@RequestMapping("/query")
public class QueryController {
    @Autowired
    private QueryService queryService;

    /**
     * 进入数据查询页面，初始化下拉框内容 andooco 20201127
     *
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/main")
    public String queryMain(Model model) throws Exception {
        log.info("进入方法QueryController.queryMain()");
        Map<String, Object> mapResult = queryService.initData();
        model.addAttribute("jdbcDtos", (List<JdbcDto>) mapResult.get("jdbcDtos"));
        model.addAttribute("funcDtos", (List<FuncDto>) mapResult.get("funcDtos"));
        if (((List<FuncDto>) mapResult.get("funcDtos")).size() > 0) {
            model.addAttribute("initSql", ((List<FuncDto>) mapResult.get("funcDtos")).get(0).getFuncContent());
        }
        log.info("保存功能块结束");
        return "/common/main";
    }

    /**
     * 查询对应sql andooco 20201127
     *
     * @param funcUid
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryThisSql")
    @ResponseBody
    public String queryThisSql(Model model, String funcUid) throws Exception {
        log.info("进入方法FuncController.queryThisSql() {}", funcUid);
        String result = queryService.queryThisSql(funcUid);
        log.info("查询sql结束：{}", result);
        return result;
    }

    /**
     * 查询数据结果 andooco 20201127
     *
     * @param uid
     * @param funcId
     * @param selectParams
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryResult")
    @ResponseBody
    public JSONObject queryResult(Model model, String uid, String funcId, String selectParams) throws Exception {
        log.info("进入方法FuncController.queryResult() {}{}{}", uid, funcId, selectParams);
        JSONObject result = queryService.queryResult(uid, funcId, selectParams);
        log.info("查询结果结束：{}", result);
        return result;
    }
}
